package com.example.yogurtsystem.controller;

import com.example.yogurtsystem.dto.ResultDTO;
import com.example.yogurtsystem.entity.Budget;
import com.example.yogurtsystem.service.BudgetService;
import com.example.yogurtsystem.service.UserRecordService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @auther lecon
 * @date 2021/8/15  13:27
 */
@CrossOrigin
@RestController
@RequestMapping("/budget")
public class BudgetController {

    @Resource
    BudgetService budgetService;

    @Autowired
    private UserRecordService userRecordService;

    @GetMapping("/insertBudgetByYear")
    @ApiOperation("添加预算表")
    public ResultDTO insertBudgetByYear(@RequestParam int year){
        int count = budgetService.insertBudgetByYear(year);
        return ResultDTO.requestSuccess(count);
    }

    @PostMapping("/updateBudget")
    @ApiOperation("修改季度预算")
    public ResultDTO updateBudget(@RequestBody Budget budget){
        budgetService.updateBudget(budget);
        return ResultDTO.requestSuccess("更新成功");
    }

    @GetMapping("/getOne")
    @ApiOperation("获取当前季度信息")
    public ResultDTO getOne(@RequestParam String id){
        return ResultDTO.requestSuccess(budgetService.getOne(id));
    }

    // todo
    @PostMapping("getBudgetData")
    public ResultDTO getBudgetData(@RequestParam("yearAndQuarter") String yearAndQuarter) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        Map<String, Object> amountSpentMap = new HashMap<>();
        Map<String, Object> budgetMap = new HashMap<>();
        // 总预算
        BigDecimal budget = budgetService.getQuarterBudget(yearAndQuarter);
        // 统计当前季度已花费的金额
        BigDecimal amountSpent = userRecordService.getAmountSpent(yearAndQuarter);
        amountSpentMap.put("name", "已花费金额");
        amountSpentMap.put("value", amountSpent);

        if (StringUtils.isEmpty(budget)){
            budget=new BigDecimal("0");
        }
        budgetMap.put("name", "剩余预算");
        budgetMap.put("value", budget.subtract(amountSpent));

        resultList.add(amountSpentMap);
        resultList.add(budgetMap);
        return ResultDTO.requestSuccess(resultList);
    }

//    @GetMapping("/getBudgetOneYear")
//    @ApiOperation("获取年季度信息")
//    public ResultDTO getBudgetOneYear(@RequestParam int year){
//        return ResultDTO.requestSuccess(budgetService.getBudget(year));
//    }
    @GetMapping("/getBudgetOneYear")
    @ApiOperation("获取全部年季度信息")
    public ResultDTO getBudgetOneYear(int pageNum, int pageSize, int year){
        return budgetService.getBudget(pageNum,pageSize,year);
    }


}
